package easy

import (
	"math"
	"sort"
)

func LastStoneWeight(stones []int) int {
	return weight(stones, len(stones))
}
func weight(stones []int, size int) int {
	if size == 0 {
		return 0
	}
	if size == 1 {
		return stones[0]
	}
	sort.Ints(stones)
	if stones[size-1] == stones[size-2] {
		stones[size-1] = math.MaxInt32
		stones[size-2] = math.MaxInt32
		stones[size-2] = math.MaxInt32
		return weight(stones, size-2)
	}
	stones[size-2] = stones[size-1] - stones[size-2]
	stones[size-1] = math.MaxInt32
	return weight(stones, size-1)
}
